package com.ruoyi.common.tools.excel.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.employee.domain.vo.EmployeeUserVo;
import com.ruoyi.erecord.employee.service.IEmployeeService;
import com.ruoyi.erecord.region.service.IRegionService;
import com.ruoyi.erecord.samplebox.domain.vo.SampleboxImportDetailVo;
import com.ruoyi.erecord.samplebox.service.ISampleboxService;

public class SampleboxExcelListener extends MyExcelListener<SampleboxImportDetailVo> {
	
	private IRegionService regionService = SpringUtils.getBean(IRegionService.class);
	private DictService dictService = SpringUtils.getBean(DictService.class);
	private IEmployeeService employeeService = SpringUtils.getBean(IEmployeeService.class);
	private ISampleboxService sampleboxService = SpringUtils.getBean(ISampleboxService.class);
	
	private String batchNo = null;
    public SampleboxExcelListener(String batchNo) {
    	this.batchNo = batchNo;
    }
    
    @Override
    public void doValidate(SampleboxImportDetailVo data, AnalysisContext context) {
    	//1.设置批次号
    	//2.校验数据 与 匹配数据字典数据 设置对应字段
    	//3.设置 处理状态 与 处理信息
    	List<String> validateList = new ArrayList<>();
    	
    	data.setBatchNo(this.batchNo);
    	
    	if(StringUtils.isEmpty(data.getSampleboxCode())) {
    		validateList.add("[ 样本框编码 ] 为空");
    	} 
    	
    	if(StringUtils.isEmpty(data.getSampleboxName())) {
    		validateList.add("[ 名称 ] 为空");
    	}
    	    	
    	     
    	if (!StringUtils.isEmpty(data.getSampleboxTypeImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("erecord_samplebox_type", data.getSampleboxTypeImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 样本框类型 ] 匹配不到数据");
    		} else {
    			data.setSampleboxType(val);
    		}
    	} else {
    		validateList.add("[ 样本框类型 ] 为空");
    	}
    	
    	if (!StringUtils.isEmpty(data.getCityPrincipalImport())) {
    		EmployeeUserVo val = employeeService.getEmployeeUserVoByName(data.getCityPrincipalImport()); 
    		if (null == val) {
    			validateList.add("[ 城市管家负责人 ] 匹配不到数据");
    		} else {
    			data.setCityPrincipal(val.getId());
    			data.setCityPrincipalPhone(val.getPhone());
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getCityStewardImport())) {
    		EmployeeUserVo val = employeeService.getEmployeeUserVoByName(data.getCityStewardImport()); 
    		if (null == val) {
    			validateList.add("[ 城市管家 ] 匹配不到数据");
    		} else {
    			data.setCitySteward(val.getId());
    			data.setCityStewardPhone(val.getPhone());
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getAskForImport())) {
    		int val = NumberUtils.toInt( this.dictService.getDictValue("erecord_samplebox_require_type", data.getAskForImport()),-1) ; 
    		if (val == -1) {
    			validateList.add("[ 要求 ] 匹配不到数据");
    		} else {
    			data.setAskFor(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getRegionName())) {
    		String val = this.regionService.getRegionIdByName(data.getRegionName());
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 所属区域 ] 匹配不到数据");
    		}else {
    			data.setRegion(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getStreetName())) {
    		String val = this.regionService.getRegionIdByName(data.getStreetName());
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 所属街道 ] 匹配不到数据");
    		}else {
    			data.setStreet(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getCommunityName())) {
    		String val = this.regionService.getRegionIdByName(data.getCommunityName());
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 所属社区 ] 匹配不到数据");
    		}else {
    			data.setCommunity(val);
    		}
    	}
    	
    	if (!validateList.isEmpty()) {
    		String errMsg = String.join(" ，", validateList);
    		excelResult.getValidateList().add(errMsg);
    		
    		data.setProcessMsg(errMsg);
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
    	} else {
    		data.setProcessMsg("成功");
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
    	}
    }

	@Override
	protected void doOnExceptionProcess(String errorMsg) {
		SampleboxImportDetailVo vo = new SampleboxImportDetailVo();
		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);
	}

}
